package top.lixunda.ecommerce.server.auth.interceptor;

import com.alibaba.cloud.seata.web.SeataHandlerInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import top.lixunda.ecommerce.server.auth.config.SwaggerApp;

/**
 * @author Xumda
 * @version time:2019/11/21 13:30
 * <p>
 * 配置一些不能自动装配或者咩有自动装配的拦截器
 *
 * 注意：WebMvcConfigurationSupport 全局只能有一个才能生效，所以将 Swagger的配置也放在了这个里面处理
 */
@SuppressWarnings("Duplicates")
@Configuration
@Slf4j
public class InterceptorConfig extends WebMvcConfigurationSupport {

    private final SwaggerApp swaggerApp;

    @Autowired
    public InterceptorConfig(SwaggerApp swaggerApp) {
        this.swaggerApp = swaggerApp;
        log.debug("拦截器全局配置器启动！");
    }

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        log.debug("开始配置拦截器！");
        log.debug("配置了一个拦截器：RequestInterceptor");
        registry.addInterceptor(new RequestInterceptor()).addPathPatterns("/**");
        log.debug("配置了一个拦截器：SeataHandlerInterceptor");
        registry.addInterceptor(new SeataHandlerInterceptor()).addPathPatterns("/**");
        log.debug("配置了一个拦截器：LogInterceptor");
        registry.addInterceptor(new LogInterceptor()).addPathPatterns("/**");
        log.debug("配置拦截器结束！");
        super.addInterceptors(registry);
    }

    /**
     * 映射资源文件
     *
     * @param registry ResourceHandlerRegistry
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.debug("开始映射资源文件！");
        log.debug("映射Swagger资源文件！");
        swaggerApp.addResourceHandlers(registry);
        log.debug("映射资源文件结束！");
        super.addResourceHandlers(registry);
    }
}
